'use strict';

const Controller = require('egg').Controller;
const OSS = require('ali-oss');

const OSSPage = new OSS({
  region: 'oss-cn-beijing',
  accessKeyId: 'LTAI5tM8EcmsJrB5FAvb2Kne',
  accessKeySecret: 'e1KnuO0cVrmV5MqReD85SZvTjP3eGD',
  bucket: '1908aday17',
});

class UpLoadController extends Controller {
  async upload() {
    const { ctx } = this;

    // 获取到前端传来的文件流信息
    const file = ctx.request.files[0];

    // 生成文件名称
    const filename = new Date().getTime() + file.filename;

    // 将文件上传到oss
    const data = await OSSPage.put(filename, file.filepath);

    ctx.body = {
      code: 1,
      data,
    };
  }
}

module.exports = UpLoadController;
